<?php
/**
 * @author GhostWei
 * @date 2012-7-16
 */
class controller_www_index extends front_controller_panel {	
	
	public function action_html() {
		$this->action_index();
	}	
	
	public function action_index() {
		$keywords = array();
		foreach((array) $this->cache()->getKeywords() AS $kw) {
			$keywords[] = $kw['name'];
		}
		$this->view->headKeywords = implode(',', $keywords);
		$this->view->headDescription = (string) $this->view->setting('description');
		$this->_crumbs()->addPage(system_nav_page::create($this->language('首页'))->setRoute('index/index'));
		$this->render();
	}
	
	public function action_list() {
		$type = $this->_param('type');
		$page = intval($this->_param('page', 1));
		$title = $this->language(($type == 'recommend' ? '推荐好图' : '最新好图'));
		$adapter = new system_paginator_adapter_concept(new picture_special_concept($this->lang));
		$adapter->setCurrentPageNumber($page);
		$where = null;
		if($type == 'recommend') {
			$where = "special.id IN(SELECT special_id FROM `picture_special_recommend`)";
		}
		$adapter->setLimit(30);
		$adapter->where("special.ptotal > 0");
		if($where !== null) {
			$adapter->where($where);
		}
		$adapter->order("special.id DESC");
		$this->view->specialList = new system_paginator($adapter);
		$this->view->title = $title;
		$this->_crumbs()->addPage(system_nav_page::create($title)->setRoute('index/list')->setParams(array('type' => $type, 'page' => $page)));
		$this->render();
	}
	
	public function action_link() {
		$key = trim($this->_param('key'));
		$concept = new resource_link_concept();
		if(!($row = $concept->fetch(array('`key` = ?' => $key)))) {
			$this->_goRoute('index/index');
		}
		$row->setClick($row->getClick() + 1);
		$row->save();
		$this->_redirector($row->getUrl());
	}
	
	public function action_error() {		
		$response = $this->getResponse();
		$exceptions = $response->getException();
		$e = $exceptions[0];
// 		try {
// 			$this->getResponse()->setHttpResponseCode($e->getCode());
// 		} catch (Exception $ex) {
// 		}
		if($e->getCode() != 404) {
			$exceptions = 'The page you requested could not be found. It is possible that the address is incorrect, or that the page no longer exists.';
		}
		$this->view->exception = $exceptions;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('错误') . ': ' . $e->getCode())->setRoute('index/error'));
		$this->render();
	}
	
	public function action_client() {
		$name = strtolower($this->_param('name'));
		if(
			(($this->client == 'wap' || $this->client == 'mobile') && ($name == 'web' || $name == 'pad') 
				|| 
				($this->client != 'wap' && $name == 'wap')
			) && !$this->getRequest()->isPost()) {
			$this->view->name = $name;
			$this->_crumbs()->addPage(system_nav_page::create($this->language('切换设备'))->setRoute('index/client')->setParams(array('name' => $name)));
			$this->render();
		} else {
			if($this->_post('submit') == 'submit') {				
				$this->_changeClient($this->_post('referer'));
			} elseif($this->_post('submit') == 'cancel') {
				$this->_redirector($this->_post('referer'));
			} else {
				$this->_changeClient($this->getRequest()->getHeader('referer'));
			}
		}
	}
	
	private function _changeClient($referer) {
		try {
			$client = new system_setting_client($this->_param('name'));
			system_passport_instance::getInstance()->getSession()->setClient(strtolower($client->getKey()));
		} catch (Exception $e) {
					
		}
		$this->_redirector($referer);
	}
	
	public function action_robots() {
		echo $this->view->setting('robots');
	}
		
	public function action_favicon() {
		$lang = system_nav_language::getInstance()->getHtmlName();
		$name = DIRECTORY_SEPARATOR . 'favicon.ico';
		$filename = _DIRECTORY_SETTING . DIRECTORY_SEPARATOR . $lang . $name;
		if(!file_exists($filename)) {
			$filename = _DIRECTORY_SETTING . $name;
		}
		$this->getResponse()->setHeader('Content-Type', system_file_mime::get('ico'));
		$this->getResponse()->setBody(file_get_contents($filename));
		$this->getResponse()->sendResponse();
	}
	
}
